package Chapter01.Homework;

import java.sql.*;

//5：统计各种性别的人数，并输出
public class Work05 {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        String url="jdbc:mysql://localhost:3306/kgc76?useSSL=false";
        Connection connection=null;
        Statement st=null;
        ResultSet rs=null;
        try {
            connection= DriverManager.getConnection(url,"root","123456");
            System.out.println(connection.getClass().getName());
            System.out.println("连接已经建立");
            st=connection.createStatement();
            System.out.println("上下文："+st.getClass().getName());
            rs=st.executeQuery("select sex,count(*) from s_student group by sex");
            while (rs.next()){
                String zhi=rs.getString(1);
                String zhi2=rs.getString(2);
                System.out.print(zhi);
                System.out.print(zhi2);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
